---
description: "Reference for Nitric's Go library - Returns a reference to the latest version of a secret, regardless of that version's ID."
---

# Go - Secret.Access()

<Note>
  This is reference documentation for the Nitric Go SDK. To learn about Secrets
  in Nitric start with the [Secrets docs](/secrets).
</Note>

Returns a the value of the latest iteration of a secret.

```go
import (
	"context"

	"github.com/nitrictech/go-sdk/nitric"
	"github.com/nitrictech/go-sdk/nitric/secrets"
)

func main() {
	secret := nitric.NewSecret("secret-name").Allow(secrets.SecretAccess)

	latestSecret := secret.Access(context.TODO())

	nitric.Run()
}
```

## Notes

`Access()` is most useful when you always want the most recent secret values from the secrets manager. Database credentials and API keys are good examples of secrets where the latest value is usually what you want.

For symmetric encryption, you'll need to retrieve the version of the secret used to _encrypt_ a value when you try to _decrypt_ it again. In those cases `Access()` isn't a good choice, use [AccessVersion()](./secret-access-version) instead.

## Examples

### Get a reference to the latest secret version

```go
import (
  "context"

  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/secrets"
)

func main() {
  secret := nitric.NewSecret("secret-name").Allow(secrets.SecretAccess)

  latest := secret.Access(context.TODO())

  nitric.Run()
}
```

### Access the latest value of a secret

```go
import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/secrets"
)

func main() {
  secret := nitric.NewSecret("secret-name").Allow(secrets.SecretAccess)

  value, err := secret.Access(context.TODO())
  if err != nil {
    return
  }

  fmt.Println(value.AsString())

  nitric.Run()
}
```

<Note>
  See [Secret.AccessVersion()](./secret-access-version) for more details.
</Note>
